package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.secsys.dto.StoFinanceperstockDTO;
import com.secsys.dto.StoFinanceprofitDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.model.TbStoFinanceprofit;
import com.secsys.model.TbStoStock;
import com.secsys.service.StoFinanceProfitService;
import com.secsys.service.StoStockService;
import com.secsys.util.DateFormatUtils;

public class StoFinanceProfitServiceImpl extends ServiceImpl<TbStoFinanceprofit,Integer> implements
		StoFinanceProfitService {
	private StoStockService stoStockService;
	
	@Override
	public List<StoFinanceprofitDTO> queryStoFinanceprofitByStockCode(String stockCode) {
		List<TbStoStock> list=stoStockService.findByProperty("from TbStoStock where stockCode=?", stockCode);
		if (null==list || list.size()==0) {
			return null;
		}
		TbStoStock tbStoStock=list.get(0);
		return queryStoFinanceprofitByTbStoStock(tbStoStock);
	}

	@Override
	public List<StoFinanceprofitDTO> queryStoFinanceprofitByTbStoStock(TbStoStock tbStoStock) {
		String hql="from TbStoFinanceprofit as tsf where tsf.tbStoStock=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, tbStoStock)); 
	}

	@Override
	public List<StoFinanceprofitDTO> queryStoFinanceprofitByDate(Date fieldDate) {
		String hql="from TbStoFinanceprofit as tsf where tsf.fieldDate=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, fieldDate)); 
	}
	
	@Override
	public List<StoFinanceprofitDTO> queryStoFinanceprofitByTbStoStockCodeAndDate(
			int stockCode, Date fieldDate) {
		TbStoStock tbStoStock=stoStockService.findByProperty("from TbStoStock where stockCode=?", String.valueOf(stockCode)).get(0);
		return queryStoFinanceprofitByTbStoStockAndDate(tbStoStock,fieldDate);
	}

	@Override
	public List<StoFinanceprofitDTO> queryStoFinanceprofitByTbStoStockAndDate(
			TbStoStock tbStoStock, Date fieldDate) {
		String hql="from TbStoFinanceprofit as tsf where tsf.tbStoStock=? and tsf.fieldDate=?";
		return foreachGetDtoFromPojo(this.find(hql, new Object[]{tbStoStock,fieldDate})); 
	}
	
	

	@Override
	public Date findLastDate() {
		String sqlString="select Max(fieldDate) from TbStoFinanceprofit";
		List<Object> oList=this.dao.findObject(sqlString);
		Date date=null;
		if (oList!=null && oList.size()>0) {
			date=(Date)oList.get(0); 
		}
		return date;
	}
	
	
	@Override
	public int countSearchWithMainProfit(Date startDate, Date endDate, double num,int updown) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.mainProfit > tsf1.mainProfit and tsf2.mainProfit/tsf1.mainProfit >?";
		}else {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.mainProfit < tsf1.mainProfit and tsf1.mainProfit/tsf2.mainProfit >?";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num});
		List<StoFinanceprofitDTO> stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		for (Object object : list) {
			StoFinanceprofitDTO stoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[0]); 
			stoFinanceprofitDTOList.add(stoFinanceperstockDTO);
		}
		return list.size();
	}
	
	@Override
	public HashMap<String, List> querySearchWithMainProfit(Date startDate,
			Date endDate, double num, int updown, int offset, int count) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.mainProfit > tsf1.mainProfit and tsf2.mainProfit/tsf1.mainProfit >? order by tsf2.mainProfit/tsf1.mainProfit desc";
		}else {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.mainProfit < tsf1.mainProfit and tsf1.mainProfit/tsf2.mainProfit >? order by tsf1.mainProfit/tsf2.mainProfit desc";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num},offset,count);
		
		List<StoFinanceprofitDTO> stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		List<StoFinanceprofitDTO> oldYearStoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoFinanceprofitDTO stoFinanceprofitDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[0]); 
			stoFinanceprofitDTOList.add(stoFinanceprofitDTO);
			
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stockDTOList.add(stockDTO);
			
			StoFinanceprofitDTO oldYearStoFinanceprofitDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[2]); 
			oldYearStoFinanceprofitDTOList.add(oldYearStoFinanceprofitDTO);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoFinanceprofitDTOList", stoFinanceprofitDTOList);
		map.put("stockDTOList", stockDTOList); 
		map.put("oldYearStoFinanceprofitDTOList", oldYearStoFinanceprofitDTOList); 
		return map; 
	}
	
	@Override
	public int countSearchWithNetGain(Date startDate, Date endDate, double num,int updown) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.netGain > tsf1.netGain and tsf2.netGain/tsf1.netGain >?";
		}else {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.netGain < tsf1.netGain and tsf1.netGain/tsf2.netGain >?";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num});
		List<StoFinanceprofitDTO> stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		for (Object object : list) {
			StoFinanceprofitDTO stoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[0]); 
			stoFinanceprofitDTOList.add(stoFinanceperstockDTO);
		}
		return list.size();
	}

	@Override
	public HashMap<String, List> querySearchWithNetGain(Date startDate,
			Date endDate, double num, int updown, int offset, int count) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and tss.id=tsf1.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.netGain > tsf1.netGain and tsf2.netGain/tsf1.netGain >? order by tsf2.netGain/tsf1.netGain desc";
		}else {
			hql="from TbStoFinanceprofit as tsf2,TbStoStock as tss,TbStoFinanceprofit as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and tss.id=tsf1.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.netGain < tsf1.netGain and tsf1.netGain/tsf2.netGain >? order by tsf1.netGain/tsf2.netGain desc";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num},offset,count);
		
		List<StoFinanceprofitDTO> stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		List<StoFinanceprofitDTO> oldYearStoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoFinanceprofitDTO stoFinanceprofitDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[0]); 
			stoFinanceprofitDTOList.add(stoFinanceprofitDTO);
			
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stockDTOList.add(stockDTO);
			
			StoFinanceprofitDTO oldYearStoFinanceprofitDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[2]); 
			oldYearStoFinanceprofitDTOList.add(oldYearStoFinanceprofitDTO);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoFinanceprofitDTOList", stoFinanceprofitDTOList);
		map.put("stockDTOList", stockDTOList); 
		map.put("oldYearStoFinanceprofitDTOList", oldYearStoFinanceprofitDTOList); 
		return map; 
	}

/**
 * mainIncome主营业务收入
 */
	@Override	 
	public HashMap<String, List> queryStoFinanceprofitBySearch(Date newDate,Date lastDate,double num) { 
		String hql="from TbStoFinanceprofit as tsf1,TbStoStock as tss,TbStoFinanceprofit as tsf2  where tss.id=tsf1.tbStoStock and " +
				" tsf1.tbStoStock=tsf2.tbStoStock and tsf1.fieldDate=? and tsf2.fieldDate=? " +
				"and tsf1.mainIncome/tsf2.mainIncome> ? order by tsf1.mainIncome/tsf2.mainIncome desc";//TbStoPriceearning
		List<Object> list=this.dao.findObject(hql, new Object[]{newDate,lastDate,num},0,10);
		List<StoFinanceprofitDTO> stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoFinanceprofitDTO stoFinanceprofitDTO=getDtoFromPojo((TbStoFinanceprofit)((Object[])object)[0]); 
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stoFinanceprofitDTOList.add(stoFinanceprofitDTO); 
			stockDTOList.add(stockDTO);
//			System.out.println("queryStoFinanceprofitBySearch(Date newDate,Date lastDate,double num)\n"+stoFinanceprofitDTO); 
		}
		
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoFinanceprofitDTOList", stoFinanceprofitDTOList);
		map.put("stockDTOList", stockDTOList); 
		return map;
	}

	/**
	 * 批量地将持久化对象POJO的List<TbStoFinanceprofit>转换成领域对象DTO的List<StoFinanceprofitDTO>
	 * @param 	List<TbStoFinanceprofit>
	 * 			持久化对象POJO的List<TbStoFinanceprofit>
	 * @return	List<StoFinanceprofitDTO>
	 * 			领域对象DTO的List<StoFinanceprofitDTO>
	 */
	private List<StoFinanceprofitDTO> foreachGetDtoFromPojo(List<TbStoFinanceprofit> tbStoFinanceprofitList){
		List<StoFinanceprofitDTO> stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		for (TbStoFinanceprofit tbStoFinanceprofit : tbStoFinanceprofitList) {
			stoFinanceprofitDTOList.add(getDtoFromPojo(tbStoFinanceprofit));
		}
		return stoFinanceprofitDTOList;
	}
	
	/**
	 * 将持久化对象POJO的TbStoFinanceprofit转换成领域对象DTO的StoFinanceprofitDTO
	 * @param 	TbStoFinanceprofit
	 * 			持久化对象POJO的TbStoFinanceprofit
	 * @return	StoFinanceprofitDTO
	 * 			领域对象DTO的StoFinanceprofitDTO
	 */
	private StoFinanceprofitDTO getDtoFromPojo(TbStoFinanceprofit tbStoFinanceprofit){ 
		StoFinanceprofitDTO stoFinanceprofitDTO =new StoFinanceprofitDTO(
				tbStoFinanceprofit.getId(),tbStoFinanceprofit.getFieldDate(),
				tbStoFinanceprofit.getMainIncome(),tbStoFinanceprofit.getMainProfit(),
				tbStoFinanceprofit.getBusinessProfit(),tbStoFinanceprofit.getManageFee(),
				tbStoFinanceprofit.getFinanceFee(),tbStoFinanceprofit.getBusinessProfit(),
				tbStoFinanceprofit.getInvestProfit(),tbStoFinanceprofit.getSubsidy(),
				tbStoFinanceprofit.getBusinessIncome(),tbStoFinanceprofit.getTotalGain(),
				tbStoFinanceprofit.getIncomeTax(),tbStoFinanceprofit.getNetGain()
		);
		return stoFinanceprofitDTO;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}

}
